home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4021 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Split Filename
  5. Date: Thu, 01 Feb 1996 11:49:40 +0200
  6. Organization: Carelcomp Forest
  7. Message-ID: <31108CB4.59ED@cmt.lpr.mail.carel.fi>
  8. References: <4e6lsr$8ar@news1.radix.net>,<31075FB4.3E53@cmt.lpr.mail.carel.fi> <DM23s4.77J@news.cern.ch>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  14.  
  15. Maurizio Loreti wrote:
  16. > In article <31075FB4.3E53@cmt.lpr.mail.carel.fi>, Ari Lukumies <aril@cmt.lpr.mail.carel.fi> writes:
  17. > >Jim Ward wrote:
  18. > >>
  19. > >> Is there a C function that will split
  20. > >> /home/file.c into /home and file.c ?
  21. > >>
  22. > >> Thanks,
  23. > >>
  24. > >> Jim Ward
  25. > >
  26. > >You could try:
  27. > >
  28. > >       char    pathandfile[] = "/home/file.c";
  29. > >       char    *path = pathandfile;
  30. > >       char    *p = strrchr(pathandfile, '/');
  31. > >       char    *filename = p + 1;
  32. > >       *p = '\0';
  33. > >
  34. > >Also, some systems have functions named makepath and splitpath.
  35. > >
  36. > >Later,
  37. > >AriL
  38. > >
  39. > >
  40. > >--
  41. > >All my opinions are mine and mine alone.
  42. > Try with pathhandfile[] = "file.c" ...  core dump.
  43.  
  44. The original poster was asking to split "/home/file.c", _not_ "file.c". In real life, 
  45. you should of course test the outcomes (and actually, char pathandfile[] = "xxx"; 
  46. doesn't work with some compilers if this is done inside a function...):
  47.  
  48.         char    pathandfile[] = "/home/file.c";
  49.         char    *path;
  50.         char    *p;
  51.         char    *filename;
  52.     if (path = pathandfile) {
  53.         if (p = strrchr(pathandfile, '/')) {
  54.             path = pathandfile;
  55.             filename = p + 1;
  56.             *p = '\0';
  57.         }
  58.         else {
  59.             path = NULL;
  60.             filename = pathandfile;
  61.         }
  62.     }
  63.  
  64. Later,
  65.  AriL
  66. -- 
  67. All my opinions are mine and mine alone.
  68.